/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jard.webshop.nbp;

import jard.webshop.util.Constants;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;

/**
 *
 * @author cjp
 */
@Entity(name = "User_table")
@NamedQueries(@NamedQuery(name=Constants.GET_ALL_USERS,query="SELECT u FROM User_table u"))
public class User implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    private String emailAddress;
    @Column(nullable = false)
    private String password; // hashsum of password
    @Column(nullable = false)
    private String name;
    // ManyToOne because the same person can be both a Customer and an Employee
    // Thus two entities can reference the same address
    @Embedded
    private Address address;
    @OneToMany(cascade = CascadeType.ALL,orphanRemoval=true)
    private List<Order> orderHistory = new ArrayList<Order>();
    @OneToMany(cascade = CascadeType.ALL,orphanRemoval=true)
    private List<Invoice> invoices = new ArrayList<Invoice>();
    @Column(nullable = false)
    private Boolean newsLetter = false;
    @Column(nullable = false)
    private Boolean valid = true;
    private Integer authLevel = 0;

    public User() {
    }

    public User(String email, String passwordHash) {
        this.emailAddress = email;
        this.password = passwordHash;
    }

    public User(String email, String passwordHash, Address addr) {
        this.emailAddress = email;
        this.password = passwordHash;
        this.address = addr;
        this.name = "Disa Faith";
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (emailAddress != null ? emailAddress.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof User)) {
            return false;
        }
        User other = (User) object;
        if ((this.emailAddress == null && other.emailAddress != null) || (this.emailAddress != null && !this.emailAddress.equals(other.emailAddress))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        String news = (newsLetter ? "Newsletter" : "No newsletter");
        String retval = "jard.webshop.nbp.User[ emailAddress=" + emailAddress + ", ";
        retval += name + ", ";
        retval += address + ", ";
        retval += news + " ]";
        return retval;
    }

    public Boolean hasPaidAllBills() {
        for (Invoice p : invoices) {
            if (p.isPaid() == false) {
                return false;
            }
        }

        return true;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public List<Order> getOrderHistory() {
        return orderHistory;
    }

    public void setOrderHistory(List<Order> orderHistory) {
        this.orderHistory = orderHistory;
    }

    /**
     * Also add Invoice to history.
     * 
     * @param order 
     */
    public void addOrder(Order order) {
        orderHistory.add(order);
        invoices.add(order.getInvoice());
    }

    public List<Invoice> getInvoices() {
        return invoices;
    }

    public void setInvoices(List<Invoice> invoices) {
        this.invoices = invoices;
    }

    public void addInvoice(Invoice invoice) {
        invoices.add(invoice);
    }

    /**
     * Tells us whether the user wants our newsletter or not
     */
    public Boolean getNewsLetter() {
        return newsLetter;
    }

    public void setNewsLetter(Boolean newsLetter) {
        this.newsLetter = newsLetter;
    }

    public Integer getAuthLevel() {
        return authLevel;
    }

    public void setAuthLevel(Integer authLevel) {
        this.authLevel = authLevel;
    }

    public Boolean isValid() {
        return valid;
    }

    public void setValid(Boolean valid) {
        this.valid = valid;
    }
    
    public Integer getOrderCount() {
        if(orderHistory != null) {
            return orderHistory.size();
        } else {
            return 0;
        }
    }
}
